package com.ruiben.mall.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruiben.mall.IUserService;
import com.ruiben.mall.mapper.UserAddressMapper;
import com.ruiben.mall.mapper.UserMapper;
import com.ruiben.mall.pojo.User;
import com.ruiben.mall.pojo.UserAddress;
import com.ruiben.mall.utils.PagedResult;
import com.ruiben.mall.vo.UserVo;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private Sid sid;


    @Override
    public int registerUser(User user) {
        user.setId(sid.nextShort());
        return userMapper.insert(user);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public User queryUserForLogin(String username, String password) {

        return getUser("username", username, password);
    }


    /**
     * @param phone
     * @param password
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public User queryPhoneForLogin(String phone, String password) {

        return getUser("phone", phone, password);
    }

    public UserAddress getDefaultAddress(String userId){
        Example example = new Example(UserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userid", userId);
        criteria.andEqualTo("isdefault",true);
        return userAddressMapper.selectOneByExample(example);
    }

    private User getUser(String usernameKey, String username, String password) {
        Example userExample = new Example(User.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo(usernameKey, username);
        criteria.andEqualTo("password", password);

        return userMapper.selectOneByExample(userExample);
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsernameIsExist(String username) {

        User user = new User();
        user.setUsername(username);

        User result = userMapper.selectOne(user);

        return result != null;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public boolean queryPhoneIsExist(String phone) {

        User user = new User();
        user.setPhone(phone);

        User result = userMapper.selectOne(user);

        return result != null;
    }


    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean updateUserInfo(User user) {

        Example userExample = new Example(User.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("id", user.getId());
        int i = userMapper.updateByExampleSelective(user, userExample);
        return i > 0;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User queryUserInfoById(String userId) {
        Example userExample = new Example(User.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("id", userId);
        return userMapper.selectOneByExample(userExample);
    }

    @Override
    public User queryUserInfoByUsername(String username) {
        Example userExample = new Example(User.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("username", username);
        return userMapper.selectOneByExample(userExample);
    }

    @Override
    public PagedResult getUserList(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<User> users = userMapper.getUserList();
        PageInfo<User> pageList = new PageInfo<>(users);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(users);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }


    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @Override
    public UserVo getUserInfo(String id) {
        return userMapper.getUserInfo(id);
    }

    @Override
    public boolean addUserAddress(UserAddress userAddress) {
        userAddress.setId(sid.nextShort());
        if (userAddress.getIsdefault()){
            Example example = new Example(UserAddress.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("userid",userAddress.getUserid());
            List<UserAddress> userAddresses = userAddressMapper.selectByExample(example);
            for (UserAddress address:userAddresses){
                if (address.getIsdefault()){
                    address.setIsdefault(false);
                    userAddressMapper.updateByPrimaryKeySelective(address);
                }
            }
        }

        int insert = userAddressMapper.insert(userAddress);
        return insert>0;
    }

    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        return userAddressMapper.getAddressList(userId);
    }

    @Override
    public boolean updateUserAddress(UserAddress userAddress) {
        int i = userAddressMapper.updateByPrimaryKeySelective(userAddress);
        return i>0;
    }

    @Override
    public boolean delUserAddress(String id) {
        int i = userAddressMapper.deleteByPrimaryKey(id);
        return i>0;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean setDefaultAddress(String userId,String addressId){
        List<UserAddress> userAddressList = getUserAddressList(userId);
        for (UserAddress address:userAddressList){
            if (address.getId().equals(addressId)){
                address.setIsdefault(true);
            } else {
                address.setIsdefault(false);
            }
            userAddressMapper.updateByPrimaryKeySelective(address);
        }

        return true;

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean forgetPass(User user) {

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", user.getUsername());
        criteria.andEqualTo("problem", user.getProblem());
        criteria.andEqualTo("answer", user.getAnswer());
        User userExample = userMapper.selectOneByExample(example);
        if (userExample!=null){
            userExample.setPassword(user.getPassword());
            int i = userMapper.updateByPrimaryKeySelective(userExample);
            return i>0;
        } else {
            return false;
        }

    }
}
